package com.lorianknights.rpg.popups
{
	
	import flash.display.MovieClip;
	import flash.text.TextField;
	import flash.events.MouseEvent;
	import com.lorianknights.rpg.popups.Popup;
	import com.lorianknights.rpg.popups.skins.DialoguePopupSkin;
	
	public class DialoguePopup extends Popup
	{
		
		private var popupSkin:DialoguePopupSkin;
		
		public function DialoguePopup()
		{
			
			super();
			
		}
		
		public override function destroy():void
		{
			
			this.popupSkin = null;
			removeChild(this.popupSkin);
			this.popupSkin.btnOk.removeEventListener(MouseEvent.CLICK, onOkClicked);
			this.popupSkin.btnOk.removeEventListener(MouseEvent.ROLL_OVER, onOkHover);
			this.popupSkin.btnOk.removeEventListener(MouseEvent.ROLL_OUT, onOkOff);
			
		}
		
		public function populatePopup(title:String, desc:String):void
		{
			
			this.popupSkin.txtTitle.text = title;
			this.popupSkin.txtDesc.text = desc;
			
		}
		
		public override function init():void
		{
			
			var assetClass:Class = null;
			
			this.popupSkin = new DialoguePopupSkin();
			addChild(this.popupSkin);
			
				if (this.popupSkin == null)
				{
					
					assetClass = getDefinitionByName(Constants.DIALOGUE_POPUP_SKIN) as Class;
					this.popupSkin.btnOk = this.popupSkin.getChildByName(Constants.BTN_OK_STR) as MovieClip;
					this.popupSkin.txtTitle = this.popupSkin.getChildByName(Constants.STR_TXT_TITLE) as TextField;
					this.popupSkin.txtDesc = this.popupSkin.getChildByName(Constants.STR_TXT_DESC) as TextField;
					this.popupSkin = addChildAt(new assetClass(), 0) as MovieClip;
					
				}
				
			this.popupSkin.btnOk.addEventListener(MouseEvent.CLICK, onOkClicked);
			this.popupSkin.btnOk.addEventListener(MouseEvent.ROLL_OVER, onOkHover);
			this.popupSkin.btnOk.addEventListener(MouseEvent.ROLL_OUT, onOkOff);
			
		}
		
		private function onOkClicked(event;MouseEvent):void
		{
			
			this.destroy();
			
		}
		
		private function onOkHover(event:MouseEvent):void
		{
			
			var mc:MovieClip = event.currentTarget as MovieClip;
			
				if (mc.currentLabel != Constants.IN)
				{
					
					mc.gotoAndPlay(Constants.IN);
					
				}
		}
		
		private function onOkOff(event:flash.events.MouseEvent):void
		{
			
			var mc:MovieClip = event.currentTarget as MovieClip;
			
				if (mc.currentLabel != Constants.OUT)
				{
					
					mc.gotoAndPlay(Constants.OUT);
					
				}
		}
	}
}